package com.qf.mvc.controller;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;

@Controller
@RequestMapping("/download")
public class DownloadController {
    @Value("${mvc.fileDir}")
    private String fileDir;

    @RequestMapping("/test1")
    public void test1(String name, HttpServletResponse resp) {
        String filepath = fileDir + name;
        System.out.println(filepath);
        try {
            resp.setHeader("content-disposition","attachment;filename="+name);
            IOUtils.copy(new FileInputStream(filepath),resp.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
